// nurse/view.js
var globalData = getApp().globalData;
var util = require('../utils/util.js');
var alert = require('../utils/alert.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item:null,
    date:"",
    month:1,
    userName:"",
    userPhone:"",
    isAgree:false,
    sum:"",
    appDomain: globalData.appDomain,
    start: util.formatDate(new Date()),
    isBook:'false'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    let requestUrl = globalData.apiDomain + "/BookPatient?id=" + options.id;
    wx.request({
      url: requestUrl,
      method:"GET",
      success:function(res){
        _this.setData({
          item:res.data.condition,
          sum: res.data.condition.aP0517
        })
        console.log(res.data.condition);
      }
    });
    this.isBook(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  //判断是否关注
  isBook:function(id){
    var userid = globalData.backUserInfo.syS0100;
    var _this = this;
    var url = getApp().globalData.apiDomain + "/BookPatient?userid=" + userid + "&patientId=" + id;
    wx.request({
      url: url,
      method:"GET",
      success:function(res){
       _this.setData({
         isBook:res.data
       })
      }
    })
  },
  //添加关注
  addFocuse:function(){
      var nurseId = this.data.item.aP0500;
      var userid = globalData.backUserInfo.syS0100;
      var _this = this;
      var url = getApp().globalData.apiDomain + "/AttentionPatient";
      wx.request({
        url: url,
        method: "POST",
        data:{
          userID: userid,
          patientID: nurseId
        },
        success: function (res) {
         if(res.data.code == 200) {
           alert.toast("收藏成功");
           _this.setData({
             isBook:"true"
           })
         }
         if(res.data.code == 300) {
           alert.toast("您已收藏过该护工");
         }
        }
      })
  },
  cancelFocuse:function(){
    var nurseId = this.data.item.aP0500;
    var userid = globalData.backUserInfo.syS0100;
    var _this = this;
    var url = getApp().globalData.apiDomain + "/AttentionPatient";
    wx.request({
      url: url,
      method: "DELETE",
      data: {
        userID: userid,
        patientID: nurseId
      },
      success: function (res) {
        if (res.data.code == 200) {
          alert.toast("取消收藏成功");
          _this.setData({
            isBook: "false"
          })
        }
      }
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  changeNum:function(e){
    this.setData({
        month: e.detail.value,
        sum: this.data.item.aP0517 * parseInt(e.detail.value)
    })
  },
  nameInput:function(e){
    this.setData({
      userName:e.detail.value
    })
  },
  phoneInput:function(e){
    this.setData({
      userPhone:e.detail.value
    })
  },
  submitPay:function(){
    if (this.data.date == "") {
      alert.toast("请选择时间");
      return;
    }
    if(this.data.userName == ""){
      alert.toast("请输入客户姓名");
      return;
    }
    
    if (parseInt(this.data.month)<=0){
      alert.toast("服务数量最低1个月");
      return;
    }
    if(this.data.userPhone==""){
      alert.toast("请输入手机号");
      return;
    }
    if (!this.data.isAgree) {
      alert.toast("请先阅读并同意《相关条款》");
      return;
    }
    var _this = this;
    wx.request({
      url: globalData.apiDomain + "/BookPatient",
      method:"POST",
      data:{
        AP1300: globalData.syS0100,
        AP1301: _this.data.userName,
        AP1303: _this.data.item.aP0501,
        AP1302:_this.data.item.aP0500,
        AP1304:_this.data.date,
        AP1306: parseInt(_this.data.month),
        AP1309: _this.data.userPhone
      },
      success:function(res){
        console.log(res)
        if(res.data.code == 200){
          wx.showModal({
            content: '预约成功，请保持电话畅通！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({ changed: true });  
              }
            }
          });
         
        }else if(res.data.code == 401){
          wx.showModal({
            content: '余额已不足，请先充值！',
            showCancel: false,
            success: function (res) {
              
            }
          });
        }else if(res.data.code == 300) {
          wx.showModal({
            content: '抱歉，该护工已被预约！',
            showCancel: false,
            success: function (res) {

            }
          });
        }

      }
    })
  }
})